﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using ECCIT.Data;
using OgilvyOne.UKNow.Globalization;
using OgilvyOne.UKNow.IDAL;
using OgilvyOne.UKNow.Model;

namespace OgilvyOne.UKNow.DAL
{
    public class RemberSetDal : IDALRemberSet
    {
        public int Language { get; set; }
        public RemberSetDal() { }

        public RemberSetDal(int languageType)
        {
            Language = languageType;
        }
        public  bool RemindarAll(int memberid)
        {
            if (CheckExist(memberid,1))//已经收藏过
            {
                return false;
            }
            else
            {
                return CheckExist(memberid) ? Update(memberid, true) : Insert(memberid, true);
            }
            
        }

        public bool ClearRemindar(int memberid)
        {
            if (CheckExist(memberid,0))//已经清理过
            {
                return false;
            }
            else
            {
                return CheckExist(memberid) ? Update(memberid, false) : Insert(memberid, false);
            }
            
        }
        public bool Set(MemberReminderSettingModel model)
        {
            return CheckExist(model.COL_MEMBER_ID) ? UpdateSet(model) : AddSet(model);
        }

        public MemberReminderSettingModel GetModal(int memberid)
        {
            var model=new MemberReminderSettingModel();
            var strSql = new StringBuilder();
            strSql.Append("select * from UKNOW_MEMBER_REMINDER_SETTING where  COL_MEMBER_ID=@COL_MEMBER_ID");

            IDataParameter[] parameters = {
					new SqlParameter("@COL_MEMBER_ID", SqlDbType.Int,4)				
					};
            parameters[0].Value = memberid;
            using (IDataReader reader = DBHelper.ExecuteReader(strSql.ToString(), parameters))
            {
                while (reader.Read())
                {
                    if(reader["COL_MEMBER_ID"]!= System.DBNull.Value)
                        model.COL_MEMBER_ID = Convert.ToInt32(reader["COL_MEMBER_ID"]);
                    if (reader["COL_REM_BY_EMAIL"] != System.DBNull.Value)
                        model.COL_REM_BY_EMAIL = Convert.ToBoolean(reader["COL_MEMBER_ID"]);
                    if (reader["COL_REM_BY_MOBILE"] != System.DBNull.Value)
                        model.COL_REM_BY_MOBILE = Convert.ToBoolean(reader["COL_REM_BY_MOBILE"]);
                    if (reader["COL_REM_EMAIL"] != System.DBNull.Value)
                        model.COL_REM_EMAIL = Convert.ToString(reader["COL_REM_EMAIL"]);
                    if (reader["COL_REM_MOBILE"] != System.DBNull.Value)
                        model.COL_REM_MOBILE = Convert.ToString(reader["COL_REM_MOBILE"]);
                    if (reader["COL_REM_ALL"] != System.DBNull.Value)
                        model.COL_REM_ALL = Convert.ToBoolean(reader["COL_REM_ALL"]);

                }

            }
            return model;
        }


        private bool AddSet(MemberReminderSettingModel model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into UKNOW_MEMBER_REMINDER_SETTING(");
            strSql.Append("COL_MEMBER_ID,COL_REM_BY_EMAIL,COL_REM_EMAIL,COL_REM_BY_MOBILE,COL_REM_MOBILE)");
            strSql.Append(" values (");
            strSql.Append("@COL_MEMBER_ID,@COL_REM_BY_EMAIL,@COL_REM_EMAIL,@COL_REM_BY_MOBILE,@COL_REM_MOBILE)");
            IDataParameter[] parameters = {
					new SqlParameter("@COL_MEMBER_ID", SqlDbType.Int,4),
					new SqlParameter("@COL_REM_BY_EMAIL", SqlDbType.Bit,1),
					new SqlParameter("@COL_REM_EMAIL", SqlDbType.NVarChar,100),
					new SqlParameter("@COL_REM_BY_MOBILE", SqlDbType.Bit,1),
					new SqlParameter("@COL_REM_MOBILE", SqlDbType.NVarChar,20)
					};
            parameters[0].Value = model.COL_MEMBER_ID;
            parameters[1].Value = model.COL_REM_BY_EMAIL;
            parameters[2].Value = model.COL_REM_EMAIL;
            parameters[3].Value = model.COL_REM_BY_MOBILE;
            parameters[4].Value = model.COL_REM_MOBILE;
           

            return DBHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters) > 0;
        }

        public bool UpdateSet(MemberReminderSettingModel model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update UKNOW_MEMBER_REMINDER_SETTING set ");
            strSql.Append("COL_REM_BY_EMAIL=@COL_REM_BY_EMAIL,");
            strSql.Append("COL_REM_EMAIL=@COL_REM_EMAIL,");
            strSql.Append("COL_REM_BY_MOBILE=@COL_REM_BY_MOBILE,");
            strSql.Append("COL_REM_MOBILE=@COL_REM_MOBILE");
           
            strSql.Append(" where COL_MEMBER_ID=@COL_MEMBER_ID ");
            IDataParameter[] parameters = {
					new SqlParameter("@COL_REM_BY_EMAIL", SqlDbType.Bit,1),
					new SqlParameter("@COL_REM_EMAIL", SqlDbType.NVarChar,100),
					new SqlParameter("@COL_REM_BY_MOBILE", SqlDbType.Bit,1),
					new SqlParameter("@COL_REM_MOBILE", SqlDbType.NVarChar,20),
					new SqlParameter("@COL_MEMBER_ID", SqlDbType.Int,4)};
            parameters[0].Value = model.COL_REM_BY_EMAIL;
            parameters[1].Value = model.COL_REM_EMAIL;
            parameters[2].Value = model.COL_REM_BY_MOBILE;
            parameters[3].Value = model.COL_REM_MOBILE;
            parameters[4].Value = model.COL_MEMBER_ID;

            return DBHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters) > 0;
        }

        private  bool Update(int memberid,bool flag)
        {
            var strSql = new StringBuilder();
            strSql.Append("update UKNOW_MEMBER_REMINDER_SETTING set COL_REM_ALL=@COL_REM_ALL where COL_MEMBER_ID=@COL_MEMBER_ID");
            IDataParameter[] parameters = {
					new SqlParameter("@COL_REM_ALL", SqlDbType.Bit),
                     new SqlParameter("@COL_MEMBER_ID", SqlDbType.Int,4)	};
            parameters[0].Value =flag;
            parameters[1].Value = memberid;
            return DBHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters) > 0;

        }

        private bool Insert(int memberid,bool flag)
        {
            var strSql = new StringBuilder();
            strSql.Append(
                "	insert into UKNOW_MEMBER_REMINDER_SETTING (COL_MEMBER_ID,COL_REM_ALL) values(@COL_MEMBER_ID,@COL_REM_ALL)");
            IDataParameter[] parameters = {
					new SqlParameter("@COL_MEMBER_ID", SqlDbType.Int,4),
                     new SqlParameter("@COL_REM_ALL", SqlDbType.Bit)	};
            parameters[0].Value = memberid;
            parameters[1].Value = flag;

            return DBHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters) > 0;


        }

        private bool CheckExist(int colMemberID,int flag)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from UKNOW_MEMBER_REMINDER_SETTING");
            strSql.Append(" where COL_MEMBER_ID=@COL_MEMBER_ID and COL_REM_ALL=@COL_REM_ALL ");
            IDataParameter[] parameters = {
					new SqlParameter("@COL_MEMBER_ID", SqlDbType.Int,4),
                                          new SqlParameter("@COL_REM_ALL", SqlDbType.Int,4)	};
            parameters[0].Value = colMemberID;
            parameters[1].Value = flag;

            using (IDataReader reader = DBHelper.ExecuteReader(strSql.ToString(), parameters))
            {
                while (reader.Read())
                {
                    return Convert.ToInt32(reader[0]) > 0;
                }
            }
            return false;

        }


        private bool CheckExist(int colMemberID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from UKNOW_MEMBER_REMINDER_SETTING");
            strSql.Append(" where COL_MEMBER_ID=@COL_MEMBER_ID ");
            IDataParameter[] parameters = {
					new SqlParameter("@COL_MEMBER_ID", SqlDbType.Int,4)			};
            parameters[0].Value = colMemberID;
           
            using (IDataReader reader = DBHelper.ExecuteReader(strSql.ToString(),parameters))
            {
                while (reader.Read())
                {
                    return Convert.ToInt32(reader[0]) > 0;
                }
            }
            return false;

        }
    }
}
